package com.custom.action.core;

import com.custom.action.core.annotations.DbRelated;
import com.custom.action.dbaction.AbstractTableModel;
import com.custom.action.util.DbUtil;
import com.custom.comm.enums.DbJoinStyle;
import com.custom.comm.utils.CustomUtil;
import com.custom.comm.utils.JudgeUtil;
import com.custom.jdbc.handler.TypeHandler;
import lombok.Getter;

import java.lang.reflect.Field;

/**
 * 对于@DbRelated注解的解析
 * @author   Xiao-Bai
 * @since  2021/12/2 16:34
 **/
@Getter
public class DbRelationParserModel<T> extends AbstractTableModel<T> {

    private final Class<T> entityClass;

    private final String joinTable;

    private final String joinAlias;

    private final Field field;

    private final String condition;

    private final DbJoinStyle joinStyle;

    private final String fieldName;

    private final String column;

    private final Class<? extends TypeHandler<?>> typeHandlerClass;


    public DbRelationParserModel(Class<T> entityClass, Field field, String table, String alias, boolean underlineToCamel) {
        this.entityClass = entityClass;
        this.setTable(table);
        this.setAlias(alias);
        DbRelated annotation = field.getAnnotation(DbRelated.class);
        this.joinTable = annotation.joinTable();
        this.fieldName = field.getName();
        if (JudgeUtil.isEmpty(annotation.field())) {
            this.column = underlineToCamel ? CustomUtil.camelToUnderline(this.fieldName) : this.fieldName;
        }else {
            this.column = annotation.field();
        }
        this.typeHandlerClass = annotation.typeHandler();
        this.field = field;
        this.joinAlias = annotation.joinAlias();
        this.joinStyle = annotation.joinStyle();
        this.condition = annotation.condition();
    }


    @Override
    public String createTableSql() {
        throw new UnsupportedOperationException();
    }


    @Override
    protected Object getValue(T x) {
        throw new UnsupportedOperationException();
    }

    @Override
    protected void setValue(T obj, Object value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String getFieldSql() {
        return DbUtil.fullSqlColumn(this.joinAlias, this.column);
    }

    @Override
    protected String getSelectFieldSql() {
        return DbUtil.sqlSelectAsWrapper(DbUtil.fullSqlColumn(this.joinAlias, this.column), this.fieldName);
    }

    @Override
    public String getSelectFieldSql(String alias) {
        throw new UnsupportedOperationException();
    }

}
